﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using SystemAnalizer.Common;

namespace SystemAnalizer.Models
{
    public class NonPeriodicMatrixModel : SystemModel
    {
        
        public NonPeriodicMatrixModel(int number_of_discretes, int systemsize)
            : base(number_of_discretes, systemsize)
        {

        }


        internal bool startMakeDiscretes()
        {
            StringBuilder errorstr = new StringBuilder();
            bool isok = true;
            double[] calcans;
            double[, ,] tempval = new double[this.number_discretes, this.system_size, this.system_size];
            this.discretemaker = new DiscreteMaker(this.m_CenterOfApproximation, this.H_step, this.number_discretes, "t");

            for (int i = 0; i < this.system_size; i++)

                for (int j = 0; j < this.system_size; j++)
                {
                    try
                    {
                        calcans = this.discretemaker.calculate(this.A_matrix_input[i, j]);
                        if (calcans != null && calcans.Length > 0)
                        {
                            for (int l = 0; l < this.number_discretes; l++)
                                tempval[l, i, j] = calcans[l];
                        }
                        else
                        {
                            errorstr.Append(string.Format("invalid input in row {0}, cell {1} \n", i, j));
                            isok = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        errorstr.Append(string.Format("invalid input in row {0}, cell {1} \n", i, j));
                        isok = false;
                    }
                }

            if (!isok)
                ErrorDialogs.PrintErrorDialog(errorstr.ToString());
            else
            {
                SetDiscrtes(tempval);
            }
            return isok;
        }

        private void SetDiscrtes(double[, ,] tempval)
        {
            this.A_descretematrix = new double[this.number_discretes, this.system_size, this.system_size];
            for (int l = 0; l < this.number_discretes; l++)
                for (int i = 0; i < this.system_size; i++)
                    for (int j = 0; j < this.system_size; j++)
                        this.A_descretematrix[l, i, j] = tempval[l, i, j];

        }
    }
}
